package com.example.floatingpopulation.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.crypto.SecureUtil;
import com.example.floatingpopulation.common.CaptchaConfig;
import com.example.floatingpopulation.common.Result;
import com.example.floatingpopulation.controller.request.AdminPageRequest;
import com.example.floatingpopulation.controller.request.LoginRequest;
import com.example.floatingpopulation.entity.Admin;
import com.example.floatingpopulation.entity.Password;
import com.example.floatingpopulation.service.AdminService;
import com.example.floatingpopulation.service.CommunityAssistanceService;
import com.example.floatingpopulation.service.FloatingPopulationService;
import com.example.floatingpopulation.service.TemporaryPopulationService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    private static final String PASS_SALT = "qiu";
    @Autowired
    AdminService adminService;
    @Autowired
    FloatingPopulationService floatingPopulationService;
    @Autowired
    TemporaryPopulationService temporaryPopulationService;
    @Autowired
    CommunityAssistanceService communityAssistanceService;



    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginRequest request, @RequestParam String key, HttpServletRequest httpRequest) {
        // 判断验证码正确性
        if (!request.getVerCode().toLowerCase().equals(CaptchaConfig.CAPTCHA_MAP.get(key))){
            // 不相等，验证不通过，清除前台的http请求内容
            CaptchaUtil.clear(httpRequest);
            return Result.error("验证码错误");
        }
        // 清除这次存储的验证码key
        CaptchaConfig.CAPTCHA_MAP.remove(key);
        return Result.success(adminService.login(request));
    }

    @PostMapping("/save")
    public Result<String> save(@RequestBody Admin obj) {
        adminService.save(obj);
        return Result.success();
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody Admin obj) {
        adminService.update(obj);
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        adminService.deleteById(id);
        return Result.success();
    }


    @GetMapping("/list")
    public Result<String> list(){
        List<Admin> list = adminService.list();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<String> getById(@PathVariable Integer id){
        Admin obj = adminService.getById(id);
        return Result.success(obj);
    }

    @PutMapping("/resetPassword")
    public Result<String> resetPassword(@RequestBody Admin admin){
        adminService.resetPassword(admin);
        return Result.success();
    }

    @PutMapping("/updatePassword")
    public Result<String> updatePassword(@RequestBody Password password){
        Admin admin = adminService.getById(Integer.valueOf(password.getId()));
        if (!SecureUtil.md5(password.getPassword() + PASS_SALT).equals(admin.getPassword())){
            return Result.error("原始密码错误，修改密码失败");
        }else {
            adminService.updatePassword(password);
        }
        return Result.success();
    }

    @GetMapping("/page")
    public Result<String> page(AdminPageRequest pageRequest){
        return Result.success(adminService.page(pageRequest));
    }

    @GetMapping("/charts")
    public Result<String> charts(){
        // 人口分类统计图
        List<Dict> list1 = floatingPopulationService.countFloatingPopulation();
        List<Dict> list2 = temporaryPopulationService.countTemporaryPopulation();
        List<Dict> bar1 = communityAssistanceService.countCommunityAssistanceStatusTrue();
        List<Dict> bar2 = communityAssistanceService.countCommunityAssistanceTodayCreate();
        Dict res = Dict.create().set("line1",list1).set("line2",list2).set("bar1",bar1).set("bar2",bar2);
        return Result.success(res);
    }
}
